package com.gettec.lims.server.util;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import com.gettec.lims.shared.model.RegSheet;
import com.googlecode.jlynx.DAO;
import com.googlecode.jlynx.DAOImpl;

public class Mysql 
{
	
private String constr="";
private static void init()
{
	Properties props = new Properties();
	props.setProperty("user", "root");
	props.setProperty("password", "root66");
	try {
		Class.forName("com.mysql.jdbc.Driver");
	} catch (ClassNotFoundException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	}
	try {
		new DAOImpl("jdbc:mysql://localhost/lims", props);
		
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}

/*static DatabaseManager dbm=DatabaseManager.getDatabaseManager("lims");
public static DatabaseManager getDbm()
{
	if(dbm==null)
	{
		 dbm=DatabaseManager
		 .getDatabaseManager("lims",20,"com.mysql.jdbc.Driver",
				 "jdbc:mysql://localhost/lims","root","root");
	}
	return dbm;
}*/
/**
 * 保存或修改单条数据
 * @param o
 */
public static int save(Object o)
{
	try {
	return	new DAOImpl(o).save();
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return 0;
}
public static int update(Object o)
{
	try {
		return new DAOImpl(o).update();
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return 0;
}
/**
 * 取多条数据
 * @param sql
 * @return
 */
public static <T> List  query(Class<T> cs, String sql)
{
	
try {
	return 	new DAOImpl(cs).fetchList(sql, null);
} catch (SQLException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}
	return new ArrayList();		
		
}
/**
 * 取单条数据
 * @param <T>
 * @param object
 * @return
 */
public static <T> void load(T object)
{
	
		try {
			 new DAOImpl(object).select();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
}
/**
 * 批量修改或删除
 * @param sql
 * @return
 */
public static int update(Class c,String sql)
{

		 try {
			new DAOImpl(c).exec(sql, null);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
	return 0;
}
/**
 * 删除单条
 * @param o
 * @return
 */
public static int delete(Object o)
{
	try {
		new DAOImpl(o).delete();
		return 1;
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return 0;
}
public static void main(String[] args) throws SQLException  {
	init();
	RegSheet rg=new RegSheet();
	rg.setAccepter("ggggggggggggg");
	new DAOImpl(rg).save();
	//DatabaseManager dbmd=getDbm();
	//query(SysUser.class, "select * from sys_user", new Object[]{});
	//System.out.println(dbmd.getDatabase().getConnection().toString());
}

}
